# -*- coding: utf-8 -*-
# Created by 'Zhou Bingbing'  on 2019/4/19
import random
from time import sleep

import execjs
from config_setting .config_setting import session
class Baidu():
    headers = {
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'Content-Length': '157',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': 'BAIDUID=959BD58239197A4C8C27D9AA6CE6802A:FG=1; BIDUPSID=959BD58239197A4C8C27D9AA6CE6802A; PSTM=1537491023; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDUSS=WxrdWdLV2pHOGU0T1RJTjZXVjZQcmtldU9kSWhFWlhpc1dzaFl3bFJLOTRCQk5kSUFBQUFBJCQAAAAAAAAAAAEAAABW3BHTaHN6YjIyNTgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHh361x4d-tcS; delPer=0; PSINO=5; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; locale=zh; yjs_js_security_passport=9281bcbb1309348f4fe490ff4b4b02d9185691ed_1563931227_js; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1563376575,1563420801,1563931224,1563931501; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1563931501; sideAdClose=18101; BDRCVFR[CZsEJLjIHxn]=mk3SLVN4HKm; H_PS_PSSID=; from_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22%3A%22wyw%22%2C%22text%22%3A%22%u6587%u8A00%u6587%22%7D%5D; to_lang_often=%5B%7B%22value%22%3A%22cs%22%2C%22text%22%3A%22%u6377%u514B%u8BED%22%7D%2C%7B%22value%22%3A%22swe%22%2C%22text%22%3A%22%u745E%u5178%u8BED%22%7D%2C%7B%22value%22%3A%22el%22%2C%22text%22%3A%22%u5E0C%u814A%u8BED%22%7D%5D'
        , 'Host': 'fanyi.baidu.com',
        'Origin': 'https: // fanyi.baidu.com',
        'Referer': 'https: // fanyi.baidu.com /?aldtype = 16047',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'
        ,'path': 'multitransapi',
    }
    proxy_list = [
        {"http": "112.122.252.161:4554"},
        {"http": "113.94.123.9:4587"},
        {"http": "113.237.255.201:4586"},
        {"http": "122.194.85.4:4531"},
        {"http": "182.243.148.86:4535"},
        {"http": "115.221.14.163:4507"},
        # {"http" : "root:01@124.88.67.81:80"}
    ]
    proxy = random.choice(proxy_list)
    @staticmethod
    def tranapi(text,FROM='zh',TO='en'):
        url='https://fanyi.baidu.com/v2transapi'
        with open('./baidujs.js')as f:
            jsData=f.read()
        inputData=text
        data={
        'from':FROM,
        'to':TO,
        'query': text,
        "sign": execjs.compile(jsData).call("e", inputData),
        "token": "1d8992194dc3afd0fbb021f6e6633b1e",
        }
        re=session.post(url, data=data, headers=Baidu.headers)
        re=re.json()
        return re['trans_result']['data'][0]['dst']
    @staticmethod
    def create_en(text,FROM='zh',TO='en'):
        url='https://fanyi.baidu.com/multitransapi'
        data={
            'from':FROM,
            'to':TO,
            'query':text,
            'raw_trans': Baidu.tranapi(text,FROM=FROM,TO=TO),
        }
        re=session.post(url,data=data,headers=Baidu.headers)
        ls=re.json()
        return ls['data']['cands']

    @staticmethod
    def create_cn(text, FROM='en', TO='zh'):
        url = 'https://fanyi.baidu.com/multitransapi'
        data = {
            'from': FROM,
            'to': TO,
            'query': text,
            'raw_trans': Baidu.tranapi(text,FROM=FROM,TO=TO),
        }
        re = session.post(url, data=data, headers=Baidu.headers)
        ls = re.json()
        return ls['data']['cands']
    @staticmethod
    def main(text):
        rest=[]
        for ii in ['en','ara','dan','kor','pt','wyw','est','de','nl','jp','spa','bul','ru','cs','swe','el','it','pl','fra','rom','slo','hu','vie','fin','th','yue','cht']:
            sleep(0.5)
            if ii=='wyw':
                re = Baidu.tranapi(text,TO=ii)
                rest.append(re)
            re=Baidu.create_en(text,TO=ii)
            if re:
                for i in re:
                    if  isinstance(Baidu.create_cn(i,FROM=ii),list):
                        rest.extend(Baidu.create_cn(i,FROM=ii))
                    else:
                        rest.append(Baidu.create_cn(i,FROM=ii))
        return list(set(rest))
